<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qml-extending.qdoc -->
  <title>Extending QML - Inheritance and Coercion Example | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qmlextendingexamples.html">Qt QML Examples</a></td><td >
			扩展QML -继承和强制示例</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><span lang="zh-cn">目录</span></h3>
<ul>
<li class="level1"><a href="#declare-boy-and-girl"><span lang="zh-cn">声明</span> Boy 
<span lang="zh-cn">和 </span>Gir<span lang="zh-cn">l类</span></a></li>
<li class="level2"><a href="#define-people-as-a-base-class"><span lang="zh-cn">
定义</span>People<span lang="zh-cn">类作为基类</span></a></li>
<li class="level2"><a href="#define-boy-and-girl"><span lang="zh-cn">定义</span>Boy
<span lang="zh-cn">和</span> Gir<span lang="zh-cn">l类</span></a></li>
<li class="level1"><a href="#running-the-example"><span lang="zh-cn">运行该示例</span></a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">扩展QML -继承和强制示例</h1>
<span class="subtitle"></span>
<!-- $$$referenceexamples/coercion-brief -->
<p>C++继承和强制。</p>
<!-- @@@referenceexamples/coercion -->
<!-- $$$referenceexamples/coercion-description -->
<div class="descr"> <a name="details"></a>
<p>这个例子建立在:</p>
<ul>
<li><a href="qtqml-referenceexamples-properties-example.html">扩展QML -对象和列表属性类型示例</a></li>
<li><a href="qtqml-referenceexamples-adding-example.html">扩展QML -添加类型示例</a></li>
</ul>
<p>继承和强制示例演示了如何使用基类将多个类型的类型分配给属性。它将前面示例中开发的Person类型专门化为两种类型—<code>Boy</code> 和<code>Girl</code>。</p>
<pre class="qml">

  <span class="type">BirthdayParty</span> {
      <span class="name">host</span>: <span class="name">Boy</span> {
          <span class="name">name</span>: <span class="string">&quot;Bob Jones&quot;</span>
          <span class="name">shoeSize</span>: <span class="number">12</span>
      }
      <span class="name">guests</span>: [
          <span class="type">Boy</span> { <span class="name">name</span>: <span class="string">&quot;Leo Hodges&quot;</span> },
          <span class="type">Boy</span> { <span class="name">name</span>: <span class="string">&quot;Jack Smith&quot;</span> },
          <span class="type">Girl</span> { <span class="name">name</span>: <span class="string">&quot;Anne Brown&quot;</span> }
      ]
  }

</pre>
<a name="declare-boy-and-girl"></a>
<h2 id="declare-boy-and-girl">声明 Boy 和 Girl类</h2>
<pre class="cpp">

  <span class="keyword">class</span> Boy : <span class="keyword">public</span> Person
  {
      Q_OBJECT
  <span class="keyword">public</span>:
      Boy(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span> parent <span class="operator">=</span> <span class="number">0</span>);
  };

  <span class="keyword">class</span> Girl : <span class="keyword">public</span> Person
  {
      Q_OBJECT
  <span class="keyword">public</span>:
      Girl(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span> parent <span class="operator">=</span> <span class="number">0</span>);
  };

</pre>
<p>Person类在本例中保持不变，而Boy和Girl 
C++类是它的简单扩展。例如，这里使用的继承有点做作，但在实际应用程序中，这两个扩展很可能会添加额外的属性或修改Person类的行为。</p>
<a name="define-people-as-a-base-class"></a>
<h3 >定义People类作为基类</h3>
<p>
自上一个示例以来，People类本身的实现没有改变。然而，由于我们已经将People类重新用途为Boy和Girl的公共基类，所以我们希望避免直接从QML实例化它——应该实例化一个显式的Boy或Girl。</p>
<pre class="cpp">

  qmlRegisterType<span class="operator">&lt;</span>Person<span class="operator">&gt;</span>();

</pre>
<p>虽然我们希望不允许从QML中实例化Person，但它仍然需要向QML引擎注册，以便它可以用作属性类型，并可以强制使用其他类型。</p>
<a name="define-boy-and-girl"></a>
<h3 ><span lang="zh-cn">定义</span> Boy 和 Girl类</h3>
<p>Boy和Girl的执行是微不足道的。</p>
<pre class="cpp">

  Boy<span class="operator">::</span>Boy(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span> parent)
  : Person(parent)
  {
  }

  Girl<span class="operator">::</span>Girl(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span> parent)
  : Person(parent)
  {
  }

</pre>
<p>所需要做的就是实现构造函数，并向QML引擎注册类型及其QML名称。</p>
<a name="running-the-example"></a>
<h2 id="running-the-example">运行该示例</h2>
<p>BirthdayParty类型自上一个示例以来没有改变。celebrant&nbsp;和guests的属性仍然使用People类型。</p>
<pre class="cpp">

      Q_PROPERTY(Person <span class="operator">*</span>host READ host WRITE setHost)
      Q_PROPERTY(<span class="type"><a href="qqmllistproperty.html">QQmlListProperty</a></span><span class="operator">&lt;</span>Person<span class="operator">&gt;</span> guests READ guests)

</pre>
<p>
但是，由于所有三种类型(Person、Boy和Girl)都已在QML系统中注册，因此在分配时QML会自动(和类型安全地)将Boy和Girl对象转换为Person。</p>
<p>示例中的main.cpp文件包含一个简单的shell应用程序，该应用程序加载并运行本页开头显示的QML代码片段。</p>
<p><span lang="zh-cn">文件：</span></p>
<ul>
<li><a href="qtqml-referenceexamples-coercion-birthdayparty-cpp.html">referenceexamples/coercion/birthdayparty.cpp</a></li>
<li><a href="qtqml-referenceexamples-coercion-birthdayparty-h.html">referenceexamples/coercion/birthdayparty.h</a></li>
<li><a href="qtqml-referenceexamples-coercion-coercion-pro.html">referenceexamples/coercion/coercion.pro</a></li>
<li><a href="qtqml-referenceexamples-coercion-coercion-qrc.html">referenceexamples/coercion/coercion.qrc</a></li>
<li><a href="qtqml-referenceexamples-coercion-example-qml.html">referenceexamples/coercion/example.qml</a></li>
<li><a href="qtqml-referenceexamples-coercion-main-cpp.html">referenceexamples/coercion/main.cpp</a></li>
<li><a href="qtqml-referenceexamples-coercion-person-cpp.html">referenceexamples/coercion/person.cpp</a></li>
<li><a href="qtqml-referenceexamples-coercion-person-h.html">referenceexamples/coercion/person.h</a></li>
</ul>
</div>
<!-- @@@referenceexamples/coercion -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
